package com.richie696.rabbit.product.controller;

import com.richie696.rabbit.commons.ExecuteResult;
import com.richie696.rabbit.product.domain.ProductInfo;
import com.richie696.rabbit.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/product")
public class ProductController {

    private final ProductService productService;

    @Autowired
    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    @RequestMapping("/i_{id}")
    public String getItem(@PathVariable Long id, Model model) {
        ExecuteResult<ProductInfo> result = productService.findProductById(id);
        if (!result.isSuccess()) {
            model.addAttribute("errorMsg", result.getErrorMessage());
        } else {
            model.addAttribute("product", result.getData());
        }
        return "/client/item";
    }

}
